package com.fenghongzhang.day009;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        try {
            URL url = new URL("");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

//            connection.setRequestMethod("POST");
//            connection.setDoInput(true);
//            connection.setDoOutput(true);
//
//            OutputStream outputStream = connection.getOutputStream();
//            String s = "ab=123&abc=123";
//            outputStream.write(s.getBytes());
//            outputStream.flush();

            connection.connect();
            connection.getResponseCode();

        } catch (MalformedURLException e) {


        } catch (IOException e) {
            e.printStackTrace();
        }


        //创建数据库
        MySqlHelp mySqlHelp = new MySqlHelp(this, "user.db", null, 1);
        //打开数据库的意识,就是得到一个db对象..增删改插的
        //都可以读写.
        //磁盘满了.
        db = mySqlHelp.getReadableDatabase();
//        mySqlHelp.getWritableDatabase();
    }

    public void create(View view) {


    }

    public void insert(View view) {
        //原生
        //bug : android.database.sqlite.SQLiteException: 1 values for 2 columns (code 1):
//        String sql = "insert into student(name) values(?)";
//        db.execSQL(sql,new Object[]{"小明"});


        //map
//        ContentValues contentValues = new ContentValues();
//        //列名-值
//        contentValues.put("name","大明");
////        contentValues.put("age",16);
//        //系统提供的方法
//        db.insert("student",null,contentValues);


        //事务
        db.beginTransaction();

        db.execSQL("insert into student(name) values(?)",new Object[]{"小小明"});
        db.delete("student","id = ?",new String[]{"4"});

        db.setTransactionSuccessful();

        db.endTransaction();


        //关闭数据库
//        db.close();




    }

    public void query(View view) {

        //1,原生查询
//        String sql = "select * from student";
////        db.execSQL(); 没有返回值
//        Cursor cursor1 = db.rawQuery(sql, null);
//        if(cursor1 != null){
//            while(cursor1.moveToNext()){
//                String id = cursor1.getString(cursor1.getColumnIndex("id"));
//                String name = cursor1.getString(cursor1.getColumnIndex("name"));
//
//                Log.i(TAG, "query: "+id+":"+name);
////
//            }
//            cursor1.close();
//        }


        //select name from student where id = ?;
        //2
        Cursor cursor = db.query("student", null,null,null,null,null,null);
        if(cursor != null){
            //移动游标
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                Log.i(TAG, "query: "+name);

            }
            cursor.close();
        }
    }

    //更新
    public void update(View view) {
        //1 原生
//        String sql = "update student set name = ? where id = ?";
//        db.execSQL(sql,new Object[]{"大明",1});

        ContentValues contentValues = new ContentValues();
        contentValues.put("name","大明");

        //2 ,update
        db.update("student",contentValues,"id = ?",new String[]{2+""});
    }

    //删除
    public void del(View view) {
//        String sql = "delete from student where id = ?";
//        db.execSQL(sql,new Object[]{1});

        //删除了多少行
        int student = db.delete("student", "id = ?", new String[]{2 + ""});


    }
}
